﻿using System;
using System.Collections.Generic;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace WAD.DAL
{
    public class DAOFCShared : DAO
    {
        public static int Insert(MFCShared m)
        {
            String sql = "insert into fc_shared([user_id],article_id,price,[platform],by_user_id,picurl,money_id,areaname,state,date)values(@user_id,@article_id,@price,@platform,@by_user_id,@picurl,@money_id,@areaname,@state,@date);select @@IDENTITY";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@article_id", DbType.Int32, m.article_id);
            db.AddInParameter(cmd, "@price", DbType.Decimal, m.price);
            db.AddInParameter(cmd, "@platform", DbType.Int32, m.platform);
            db.AddInParameter(cmd, "@by_user_id", DbType.Int32, m.by_user_id);
            db.AddInParameter(cmd, "@picurl", DbType.String, m.picurl);
            db.AddInParameter(cmd, "@money_id", DbType.String, m.money_id);
            db.AddInParameter(cmd, "@areaname", DbType.String, m.areaname);
            db.AddInParameter(cmd, "@state", DbType.Int32, m.state);
            db.AddInParameter(cmd, "@date", DbType.DateTime, m.date);
            object o = db.ExecuteScalar(cmd);
            if (o == null) return 0;
            return Convert.ToInt32(o);
        }

        /// <summary>
        /// 更新分享资料(超过30分钟分享时调用)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="money_id"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static int UpdateShared(int id, int money_id, DateTime date)
        {
            return ExecuteNonQuery(string.Format("update fc_shared set money_id={0},date='{1}' where id={2}", money_id, date.ToString(), id));
        }

        /// <summary>
        /// 分享并设置截图路径、分享价格(若第N次上传图片，则只更新图片内容，参数price可以为0)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="picurl"></param>
        /// <param name="price"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public static int Shared(int id, string picurl, decimal price, int state = 1)
        {
            return ExecuteNonQuery(string.Format("update fc_shared set price={0},picurl='{1}',state={2} where id={3}", price, picurl, state, id));
        }
        public static int Shared(int id, string picurl, int state = 1)
        {
            return ExecuteNonQuery(string.Format("update fc_shared set picurl='{0}',state={1} where id={2}", picurl, state, id));
        }

        public static int Shared_No(int id, string picurl, int state = 1)
        {
            return ExecuteNonQuery(string.Format("update fc_shared set picurl='{0}',state={1} where id={2}", picurl, state, id));
        }

        /// <summary>
        /// 获取某一篇文章的ID
        /// </summary>
        /// <param name="user_id"></param>
        /// <param name="article_id"></param>
        /// <param name="by_user_id"></param>
        /// <returns></returns>
        public static int GetLastId(int user_id, int article_id, int by_user_id)
        {
            object o = ExecuteScalar(string.Format("select MAX(id) from fc_shared where user_id={0} and article_id={1} and by_user_id={2}", user_id, article_id, by_user_id));
            if (o != DBNull.Value)
                return Convert.ToInt32(o);
            return 0;
        }

        /// <summary>
        /// 修改通过状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isPass">0 未审核,1 已通过,2 未通过, 3审核中</param>
        /// <param name="picurl"></param>
        /// <returns></returns>
        public static bool changeIsPass(int id, int isPass, string picurl = "")
        {
            //is_pass  0 未审核,1 已通过,2 未通过, 3审核中
            StringBuilder sb = new StringBuilder();
            sb.Append("update fc_shared set is_pass =" + isPass);

            if (!string.IsNullOrEmpty(picurl))
                sb.AppendFormat(",picurl='{0}'", picurl);

            sb.Append(" where id=" + id);

            return ExecuteNonQuery(sb.ToString()) > 0;
        }

        /// <summary>
        /// 获取通过、未通过的条数
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public static int GetCount_No(int by_user_id)
        {
            return GetCount("fc_shared", "by_user_id=" + by_user_id + " and is_pass=2");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<Dictionary<string, object>> GetListAllByArticleId(int articleId, string is_pass)
        {
            if (is_pass.IndexOf(",") > -1)
                return DAO.GetList(string.Format("select id,by_user_id,picurl,state,is_pass from fc_shared where article_id ={0} and is_pass in({1}) order by date", articleId, is_pass));
            else
                return DAO.GetList(string.Format("select id,by_user_id,picurl,state,is_pass from fc_shared where article_id ={0} and isnull(is_pass,0)={1} order by date", articleId, is_pass));
        }

        public static List<Dictionary<string, object>> GetListOld(int by_user_id, string article_ids, string fields = "*")
        {
            return DAO.GetList(string.Format("select top 100 {0} from fc_shared where by_user_id={1} and article_id in({2})", fields, by_user_id, article_ids));
        }
    }
}